package com.pzhuweb.sgblog.controller;

import com.pzhuweb.sgblog.domain.entity.User;
import com.pzhuweb.sgblog.domain.enums.AppHttpCodeEnum;
import com.pzhuweb.sgblog.exception.SystemException;
import com.pzhuweb.sgblog.service.BlogLoginService;
import com.pzhuweb.sgblog.domain.utils.ResponseResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author 鄧浩奇
 * @Classname BlogLoginController
 * @Description
 * @Date 2023/5/10 21:09
 **/

@RestController
public class BlogLoginController {

    @Resource
    private BlogLoginService blogLoginService;

    @PostMapping("/login")
    public ResponseResult<?> login(@RequestBody User user) { // 表示这个参数从请求体中获得
        if (!StringUtils.hasText(user.getUserName())) {
            // 用户名为空
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return blogLoginService.login(user);

    }

    @PostMapping("/logout")
    public ResponseResult logout() {
        return blogLoginService.logout();
    }
}
